package com.hqyj.springBoot.config;


import com.hqyj.springBoot.interceptor.ViewMappingInterceptor;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//覆盖之前的目标对象
@AutoConfigureAfter(value = WebMvcAutoConfiguration.class)

//继承WebMvcConfigurer,实现addResourceHandlers资源控制器方法
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private ResourceConfigBean resourceConfigBean;

    @Autowired
    private ViewMappingInterceptor viewMappingInterceptor;

    /**
     * @Bean ：把方法的返回值注册到容器
     * @return
     */
    @Bean
    public Connector connector(){
        Connector connector = new Connector();
        connector.setScheme("http");
        connector.setPort(80);
        return connector;
    }

    @Bean
    public ServletWebServerFactory servletWebServerFactory(){
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addAdditionalTomcatConnectors(connector());
        return factory;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //固定写法
        String osName = System.getProperty("os.name");

        if (osName.toLowerCase().startsWith("win")){
            //将本地文件夹,注册为系统的一个路径
            registry
                    //添加资源处理程序以提供静态资源。为匹配指定 URL 路径模式之一的请求调用处理程序
                    .addResourceHandler(resourceConfigBean.getResourcePartern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + resourceConfigBean.getWindowsLocal());

        }else {
            //将本地文件夹,注册为系统的一个路径
            registry
                    //添加资源处理程序以提供静态资源。为匹配指定 URL 路径模式之一的请求调用处理程序
                    .addResourceHandler(resourceConfigBean.getResourcePartern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + resourceConfigBean.getLinuxLocal());

        }
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//匹配所有路径
                .allowedOrigins("*")//所有的源都可以取
                .allowedMethods("POST","GET","PUT","DELETE","OPTIONS")//允许所有的方法
                .maxAge(3600)//生命周期,单位秒
                .allowedOriginPatterns("*");//也是允许所有路径,版本有所不同
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(viewMappingInterceptor).addPathPatterns("/**");
    }
}
